package com.jfinal.demo.web.controller;

import javax.servlet.http.HttpSession;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.demo.core.entity.User;
import com.jfinal.demo.web.validator.LoginValidator;
import com.jfinal.ext.render.CaptchaRender;

/**
 * 登录 controller。模拟登录
 * 
 * @author Administrator
 * 
 */
public class LoginController extends Controller {
	
	public static final String RANDOM_CODE_KEY = "systemRandomCode";

	public void index() {
		render("index.jsp");
	}

	/**
	 * 登录。
	 */
	@Before(LoginValidator.class)
	public void login() {
		String username = getPara("username");
		String password = getPara("password");
		User user = new User(username, password);
		getSession(true).setAttribute("user", user);
		redirect("/member/list");
	}

	/**
	 * 注销
	 */
	public void logout() {
		HttpSession session = getSession();
		try {
			session.invalidate();
		} catch (Exception e) {
		}
		redirect("/");
	}
	
	public void captcha() {
		render(new CaptchaRender(RANDOM_CODE_KEY));
	}
}
